package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.impl.TaximeterDaoImpl;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.entity.Taximeter;
import benny_driving_ser.util.FormatUtil;
import benny_driving_ser.util.SessionUtil;

public class TaximeterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8288237924005244045L;

	/**
	 * Constructor of the object.
	 */
	public TaximeterServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// doPost(request, response);// 有了它，doGet无法print
		String action;
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if ("jzsj".equals(action.trim())) {
			out.print(System.currentTimeMillis());
		}
		if ("user-jjqcx".equals(action.trim())) {
			String userid, formid;
			if (null == (userid = request.getParameter("userid"))
					|| null == (formid = request.getParameter("formid")))
				return;
			Taximeter tm = TaximeterDaoImpl.getInstance().getTaximeter(
					Integer.parseInt(userid.trim()),
					Integer.parseInt(formid.trim()));
			JSONObject j = new JSONObject();
			try {
				j.put("formstate", tm.getFormstate() + "");
				j.put("sm", tm.getSm() + "");
				j.put("sop", tm.getSop() + "");
				j.put("sowp", tm.getSowp() + "");
				j.put("sowt", tm.getSowt() + "");
				j.put("up", tm.getUp() + "");
				j.put("period", tm.getPeriod() + "");
				j.put("total", tm.getTotal() + "");
				j.put("state", tm.getState() + "");
				System.out.println("-4-");
				out.print(j.toString());
			} catch (JSONException e) {
				e.printStackTrace();
				out.print("{'formstate':-1,'sm':-1,'sop':-1,'sowp':-1,'sowt':-1,'up':-1,'period':-1,'total':-1,'state':-1}");
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action;
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if ("dri-jjqkz".equals(action)) {
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				try {
					long res = 1;
					int driid = Integer.parseInt(sad.getValue());
					int opp = Integer.parseInt(request.getParameter("opp")
							.trim());
					String loc = request.getParameter("loc").trim();
					switch (opp) {
					case 1:
						res = TaximeterDaoImpl.getInstance()
								.ctrlTaximeterStart(driid, loc);
						break;
					case 2:
						res = TaximeterDaoImpl.getInstance()
								.ctrlTaximeterPause(driid, loc);
						break;
					case 3:
					case 4:
					case 5:
						res = TaximeterDaoImpl.getInstance().ctrlTaximeterStop(
								driid, loc, opp);
						break;
					}
					out.print(res);
				} catch (NumberFormatException e) {
					e.printStackTrace();
					System.out.println(e.toString());
					out.print(2);
				}
			} else {
				out.print(-1);
			}

		}
		if ("dri-updatejjq".equals(request.getParameter("action"))) {
			String sm, sop, sowp, sowt, up, total, state, period;
			if (null == (sm = request.getParameter("sm"))
					|| null == (sop = request.getParameter("sop"))
					|| null == (sowp = request.getParameter("sowp"))
					|| null == (sowt = request.getParameter("sowt"))
					|| null == (up = request.getParameter("up"))
					|| null == (period = request.getParameter("period"))
					|| null == (total = request.getParameter("total"))
					|| null == (state = request.getParameter("state")))
				return;
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				int res = TaximeterDaoImpl.getInstance().updateTaximeter(
						Integer.parseInt(sad.getValue()), sm.trim(),
						sop.trim(), sowp.trim(), sowt.trim(),
						Integer.parseInt(up.trim()),
						Integer.parseInt(period.trim()), total.trim(),
						Integer.parseInt(state.trim()));
				out.print(res);
				System.out.println(res);
			} else
				out.print(-1);

		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
